<template>
  <div
    class="content"
    :style="{ backgroundImage: `url(${newimageUrl})` }"
    @click="moreClickEvent()"
  >
    <div class="logo">
      <img src="~@/assets/logo/logo.png" alt="" />
      <span>江都建设</span>
    </div>
    <div class="bottom">技术支持：特提工科</div>

    <VueDragResize
      :isResizable="isDraggable"
      :isDraggable="isDraggable"
      :x="x1"
      :y="y1"
      w="auto"
      h="auto"
      dragHandle=".main"
      @dragging="dragging"
      v-if="isshow"
    >
      <div
        class="main"
        @click.stop
        :style="{ top: `${y1}px`, left: `${x1}px` }"
      >
        <div class="btn2" @click="closeEdit" v-if="isDraggable">取消</div>
        <div class="btn1" @click="sureEdit" v-if="isDraggable">确定</div>
        <div class="title">江都建设资金往来账管理系统</div>
        <div class="center">
          <div class="list">
            <div class="right">
              <input
                type="text"
                placeholder="请输入账号"
                v-model="loginForm.username"
                @keyup.enter="handleLogin"
              />
            </div>
          </div>
          <div class="list">
            <div class="right">
              <input
                type="password"
                placeholder="请输入密码"
                v-model="loginForm.password"
                @keyup.enter="handleLogin"
              />
            </div>
          </div>
          <!-- <div class="list">
            <div class="list-code">
              <input
                type="text"
                placeholder="请输入验证码"
                v-model="loginForm.code"
                @keyup.enter="handleLogin"
              />
            </div>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" />
            </div>
          </div> -->
          <div v-if="!loading" class="btn" @click.prevent="handleLogin">
            登&nbsp;&nbsp;录
          </div>
          <div class="btn" v-else>登 录 中...</div>
        </div>
      </div>
    </VueDragResize>
  </div>
</template>
<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { getRouters } from "@/api/menu";
import VueDragResize from "vue-drag-resize";
import request from "@/utils/request";
import Qs from "qs";

export default {
  name: "Login",
  components: {
    VueDragResize,
  },
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        // username: "admin",
        // password: "admin123",
        username: "",
        password: "",
        rememberMe: true, //记住密码
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
        code: [
          { required: true, trigger: "change", message: "验证码不能为空" },
        ],
      },
      loading: false,
      redirect: undefined,
      isDraggable: false,
      x: 1200,
      y: 250,
      x1: 10,
      y1: 10,
      newimageUrl: require("@/assets/logo/bg.jpg"),
      moreClickEvent: this.moreClick(),
      isshow: false,
    };
  },
  props: {
    imageUrl: {
      default: "",
      type: String,
    },
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
    imageUrl(val) {
      this.newimageUrl = val;
    },
  },
  created() {
    // this.getCode();
    this.getCookie();
    this.initLogin();
    this.getImage();
  },
  beforeDestroy() {
    this.moreClickEvent = null;
  },
  methods: {
    moreClick() {
      let timer = null;
      let num = 0;
      function f1() {
        window.clearTimeout(timer);
        num++;
        if (num > 6) {
          this.isDraggable = true;
        }

        timer = window.setTimeout(() => {
          num = 0;
        }, 500);
      }
      return f1;
    },
    sureEdit() {
      request({
        url: "/system/company/edit",
        method: "post",
        headers: {
          isToken: false,
        },
        // headers: { "content-type": "application/x-www-form-urlencoded" },
        data: {
          id: 1,
          remark: `${this.x1},${this.y1}`,
        },
      }).then((res) => {
        this.$message({
          type: "success",
          message: "保存成功!",
        });
        this.closeEdit()
      });
    },
    closeEdit() {
      this.isDraggable = false;
    },
    getImage() {
      request({
        url: "/system/company/getCompanyInfo",
        method: "post",
        headers: {
          isToken: false,
        },
      }).then((res) => {
        console.log(res);
        if (res.data && res.data.companyAddr) {
          this.newimageUrl = res.data.companyAddr;
        }
        if (res.data && res.data.remark) {
          try {
            const keys = res.data.remark.split(",");
            this.x1 = Number(keys[0] || 0) || 0;
            this.y1 = Number(keys[1] || 0) || 0;
            this.isshow = true;
          } catch (error) {
            this.isshow = true;
          }
        }
        this.isshow = true;
      });
    },
    initLogin() {
      // this.x1 = this.computedLength(this.x);
      // this.y1 = this.computedLength(this.y);
    },
    computedLength(l) {
      const w = document.body.clientWidth;
      return (l * w) / 1920;
    },
    dragging(...msg) {
      console.log(msg);
      this.x1 = msg[0].left;
      this.y1 = msg[0].top;
    },
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      if (!this.loginForm.username || this.loginForm.username === "") {
        this.$message.error("用户名不能为空");
        return;
      }
      if (!this.loginForm.password || this.loginForm.password === "") {
        this.$message.error("密码不能为空");
        return;
      }
      // if (!this.loginForm.code || this.loginForm.code === "") {
      //   this.$message.error("验证码不能为空");
      //   return;
      // }
      this.loading = true;
      if (this.loginForm.rememberMe) {
        Cookies.set("username", this.loginForm.username, { expires: 30 });
        Cookies.set("password", encrypt(this.loginForm.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", this.loginForm.rememberMe, {
          expires: 30,
        });
      } else {
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      this.$store
        .dispatch("Login", this.loginForm)
        .then(() => {
          this.getRouters();
        })
        .catch(() => {
          this.loading = false;
          this.getCode();
        });
    },
    getRouters() {
      getRouters().then((res) => {
        if (res.data && res.data.length > 0) {
          let url = "";
          url = res.data[0].path;
          if (res.data[0].children) {
            url = res.data[0].path + "/" + res.data[0].children[0].path;
          }
          // this.$router.push({ path: this.redirect || "/" });

          url = url.replace(/(\/\/)/g, "/");
          window.redirectURL = url;
          window.localStorage.setItem("redirectURL", redirectURL);
          this.$router.push({ path: url || "/" });
        } else {
          this.$message.error("无菜单权限，请联系管理员设置");
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.vdr.active:before {
  outline: none;
}
@function vw($n) {
  @return ($n * 100vw / 1920);
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
input[type="number"] {
  -moz-appearance: textfield;
}
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

[data-tip] {
  position: relative;

  &::after {
    position: absolute;
    left: 0;
    top: 76px;
    content: attr(data-tip);
    font-size: 12px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: rgba(227, 78, 39, 1);
  }
}

.content {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  // background-image: url(~@/assets/logo/bg.jpg);
  background-size: 100% 100%;

  .logo {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 64px;

    background: rgba(0, 68, 132, 0.5);
    // filter: blur(10px);
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 32px;

    img {
      width: 40px;
      height: 40px;
    }

    span {
      margin-left: 8px;

      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 24px;
      color: #ffffff;
      letter-spacing: 4px;
      opacity: 1;
    }
  }

  .bottom {
    position: absolute;
    bottom: vw(40);
    left: 0;
    width: 100%;
    text-align: center;
    font-size: vw(14);
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: rgba(255, 255, 255, 0.4);
  }

  @media screen and (max-width: 500px) {
    .main {
      width: 410px !important;
    }
  }

  .main {
    @function vw($n) {
      @return ($n * 100vw / 2200);
    }
    // position: absolute;
    // right: vw(240);
    // top: 50%;
    // top: 50%;
    // left: 50%;
    // transform: translateY(-50%);
    width: vw(520);
    height: vw(400);
    overflow: hidden;
    // height: 504px;
    // background: rgba(255, 255, 255, 1);
    // box-shadow: 0 vw(4) VW(16) rgba(53, 62, 71, 0.08);
    background-image: url(~@/assets/logo/imgs/loginformbg.png);
    background-size: 100% 100%;
    // border-radius: VW(8);
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    padding: 0 vw(40);
    position: fixed;

    .btn1 {
      position: absolute;
      top: vw(0);
      left: vw(140);
      width: vw(100);
      text-align: center;
      height: vw(36);
      background: #0470d4;
      border-radius: vw(4);
      font-size: vw(14);
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: vw(36);
      color: #ffffff;
      letter-spacing: vw(4);
      margin-top: vw(28);
      user-select: none;
      cursor: pointer;

      &:active {
        opacity: 0.8;
      }
    }

    .btn2 {
      position: absolute;
      top: vw(0);
      left: vw(300);
      width: vw(100);
      text-align: center;
      height: vw(36);
      border: 1px solid #0470d4;
      color: #0470d4;
      border-radius: vw(4);
      font-size: vw(14);
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: vw(36);
      letter-spacing: vw(4);
      margin-top: vw(28);
      user-select: none;
      cursor: pointer;

      &:active {
        opacity: 0.8;
      }
    }

    .title {
      width: 100%;
      text-align: center;
      font-size: vw(20);
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #ffffff;
      margin: vw(64) 0 vw(28) 0;
    }

    .center {
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
    }

    input {
      width: 100%;
      height: 100%;
      outline: none;
      border: none;
      padding: 0 vw(16);
      font-size: vw(18);
      font-family: Arial;
      font-weight: 400;
      line-height: vw(56);
      color: #ffffff;
      background: rgba(48, 153, 249, 0.24);
      border-radius: vw(4);

      &::-webkit-input-placeholder {
        color: rgba(255, 255, 255, 0.64);
      }

      &::-moz-placeholder {
        color: rgba(255, 255, 255, 0.64);
      }

      &:-ms-input-placeholder {
        color: rgba(255, 255, 255, 0.64);
      }
    }

    input:focus {
      border: none;
    }

    .list {
      width: 100%;
      height: vw(56);
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      margin: vw(12) 0;

      &-code {
        width: vw(264);
        // width: 520px;
        height: 100%;
        // background: rgba(255, 255, 255, 1);
        // border: 1px solid rgba(232, 238, 242, 1);
        // // border-left: 0;
        // opacity: 1;
        // border-radius: 0 4px 4px 0;
        // margin-right: 10px;
      }

      .login-code {
        width: calc(100% - #{vw(284)});
        height: 100%;
        line-height: vw(50);
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;

        img {
          width: 100%;
          height: auto;
          cursor: pointer;
        }
      }
      // }

      .right {
        width: 100%;
        height: 100%;
        // background: rgba(255, 255, 255, 1);
        // border: 1px solid rgba(232, 238, 242, 1);
        // border-left: 0;
        // opacity: 1;
        // border-radius: 0 4px 4px 0;
      }
    }

    .btn {
      width: 100%;
      text-align: center;
      height: vw(56);
      background: #0470d4;
      border-radius: vw(4);
      font-size: vw(20);
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: vw(56);
      color: #ffffff;
      letter-spacing: vw(4);
      margin-top: vw(28);
      user-select: none;
      cursor: pointer;

      &:active {
        opacity: 0.8;
      }
    }
  }
}
</style>
